package com.swf.seed.os.util;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring 上下文工具类
 *
 * @author gavin
 * @since 2.0.0
 * @create 14-7-8
 */
public class SpringContextUtils {

    private static ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("springContext-os.xml");

    static void register(ConfigurableApplicationContext context) {
        SpringContextUtils.context = context;
    }

    public static ConfigurableApplicationContext getContext() {
        if(SpringContextUtils.context==null) {
            throw new NullPointerException("Spring 容器尚未初始化！");
        }
        return SpringContextUtils.context;
    }

    
    public static Object getBean(String beanName){
    	return getContext().getBean(beanName);
    }
    public static <T> T getBean(Class<T> requiredType){
    	return getContext().getBean(requiredType);
    }
    public static <T> T getBean(String beanName,Class<T> requiredType){
    	return getContext().getBean(beanName, requiredType);
    }
    public static <T> T getBean(String beanName,Class<T> requiredType,String defaultName){
    	if (getContext().containsBean(beanName)){
    		return getContext().getBean(beanName, requiredType);
    	}
    	return getContext().getBean(defaultName, requiredType);
    }
    
    public static  <T> Map<String, T> getBeansOfType(Class<T> type){
    	return getContext().getBeansOfType(type);
    }
    
}
